<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="企业名称">
        <el-input
          v-model="formInline.name"
          placeholder="根据企业名称搜索"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门代码">
        <el-input
          v-model="formInline.department"
          placeholder="根据部门代码搜索"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="chongzhi">重置</el-button>
        <el-button type="primary" @click="removeall">批量删除</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      :data="resList"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="sel"
    >
      <el-table-column type="selection" width="55"> </el-table-column>

      <el-table-column prop="name" label="企业名称" width="120">
      </el-table-column>
      <el-table-column prop="department" label="部门代码" width="120">
      </el-table-column>
      <el-table-column label="时间" width="200">
        <template slot-scope="scope">{{ time(scope.row.time) }}</template>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            @click="bianji(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            @click="remove(scope.$index)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->

    <el-pagination
      background
      @size-change="fenye"
      @current-change="Fen"
      :current-page="pageNum"
      layout="prev, pager, next"
      :page-size="pageSize"
      :total="list.length"
    >
    </el-pagination>

    <!-- bianji -->
    <el-dialog title="修改信息" :visible.sync="show">
      <el-form :model="form">
        <el-form-item label="企业名称">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门代码">
          <el-input v-model="form.department" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="XG">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      list: [],
      coplist: [],
      pageNum: 1,
      pageSize: 4,
      form: {
        id: "",
        name: "",
        department: "",
      },
      show: false,
      formInline: {
        name: "",
        department: "",
      },
      removeIds: "",
    };
  },
  mounted() {
    this.$axios.get("/data.json").then((res) => {
      console.log(res);
      this.list = res.data;
      this.coplist = res.data;
      console.log(this.list);
    });
  },
  methods: {
    time(val) {
      var x = new Date(val * 1000).toLocaleString();
      return x;
    },
    fenye(val) {
      this.pageSize = val;
    },
    Fen(val) {
      this.pageNum = val;
    },
    bianji(val) {
      //   console.log(val);
      this.show = true;
      this.form.name = val.name;
      this.form.id = val.id;
      this.form.department = val.department;
    },
    XG() {
      this.list.forEach((item) => {
        console.log(item);
        if (item.id == this.form.id) {
          item.name = this.form.name;
          item.department = this.form.department;
        }
      });
      this.show = false;
    },
    remove(index) {
      this.list.splice(index, 1);
    },
    search() {
      console.log(1111);
      var res = this.coplist.filter((item) => {
        console.log(item);
        return (
          item.department.includes(this.formInline.department) &&
          item.name.includes(this.formInline.name)
        );
      });
      this.list = res;
    },
    sel(val) {
      let res = val.map((item) => {
        return item.id;
      });
      this.removeIds = res.join(",");
    },
    removeall() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
            this.list=this.list.filter(item=>{
                if(this.removeIds.indexOf(item.id)==-1){
                    return item
                }
            })

          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    chongzhi(){
        this.list=this.coplist
        this.formInline.name=""
        this.formInline.department=""
    }
  },
  computed: {
    resList() {
      return this.list.slice(
        this.pageSize * (this.pageNum - 1),
        this.pageSize * this.pageNum
      );
    },
  },
};
</script>